package com.qiyuesuo.service.impl;

import com.qiyuesuo.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public boolean valid(String username, String accessToken) {
        String value = stringRedisTemplate.opsForValue().get("user_" + username + "_UUID");
        return accessToken.equals(value);
    }

    @Override
    public String upload(String file) {
        String uuid = UUID.randomUUID().toString();
        stringRedisTemplate.opsForValue().set(FILE_PREFIX + "_" + uuid, file);
        return uuid;
    }

    @Override
    public String fetch(String fileId) {
        return stringRedisTemplate.opsForValue().get(FILE_PREFIX + "_" + fileId);
    }
}
